﻿// See https://aka.ms/new-console-template for more information
using TouchSocket.Core;
using TouchSocket.Sockets;

Console.WriteLine("Hello, World!");

CreateService();

Console.ReadKey();

static TcpService CreateService()
{
    var service = new TcpService();
    service.Connecting = (client, e) =>
    {
        Console.WriteLine("Connecting...");
        return EasyTask.CompletedTask;
    };//有客户端正在连接
    service.Connected = (client, e) => { Console.WriteLine("Connected..."); return EasyTask.CompletedTask; };//有客户端成功连接
    service.Disconnected = (client, e) => { Console.WriteLine("Disconnected..."); return EasyTask.CompletedTask; };//有客户端断开连接
    service.Received = (client, e) => { Console.WriteLine("Received"); return EasyTask.CompletedTask; };

    service.Setup(new TouchSocketConfig()//载入配置
                                         //.UseAspNetCoreContainer(new ServiceCollection())//使用其他Ioc
        .SetListenIPHosts("tcp://127.0.0.1:7789", 7790)//同时监听两个地址
        .ConfigureContainer(a =>//容器的配置顺序应该在最前面
                {
                    a.AddConsoleLogger();//添加一个控制台日志注入（注意：在maui中控制台日志不可用）
                    a.RegisterSingleton(service);//将服务器以单例注入。便于插件或其他地方获取。
                })
       )
        .Start();//启动

    //.ConfigurePlugins(a =>
    // {
    //     a.Add<ClosePlugin>();
    //     a.Add<TcpServiceReceivedPlugin>();
    //     a.Add<MyServicePluginClass>();
    //     //a.Add();//此处可以添加插件
    // })
    return service;
}